﻿@model IEnumerable<KCMS.BusinessLogic.Models.OrderItem>
@using KCMS.Helpers
<script type="text/javascript">
    function reload() {
        window.location.reload();
    }
</script>
<table>
    <tr>
        <th>
            Article
        </th>
        <th>
            Name
        </th>
        <th>
            Size
        </th>
        <th>
            ProductImage
        </th>
        <th>
            Quantity
        </th>
        <th>
            Price
        </th>
        <th>
        </th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Product.Article)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Product.Title)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Size.Title)
            </td>
            <td>
                <img src="@Url.ContentImageUpload(item.ProductImage.ColorImageFile)" style="width:60px;" />
            </td>
            <td class="quantity">
                @using (Html.BeginForm())
                {
                    @Html.EditorFor(modelItem => item.Quantity)
                    @Html.HiddenFor(modelItem => item.Id)
                }
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Product.Price)
            </td>
            <td>
                @Ajax.ActionLink("Delete", "Delete", new { id = item.Id }, new AjaxOptions {HttpMethod="POST", OnSuccess = "reload" })
            </td>
        </tr>
    }
</table>
<input type="button" id="save" value="Сохранить изменения" />
<script type="text/javascript">
    $(document).ready(function () {
      $("#save").live("click", function (){
        var items = [];
        
        $.each($(".quantity"), function () {
            var form = $(this).children("form");
            var orderItem = {};
            orderItem.Id = form.find("input[type='hidden'][name='item.Id']").val();
            orderItem.Quantity = form.find("input[name='item.Quantity']").val();
            items.push(orderItem);
        });
        $.ajax({
            url: "@Url.Action("Save", "OrderItem")",
            type: "POST",
            data: JSON.stringify(items),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function () {
                window.location.reload()
            }
        });
        return false;
        });
    });
   
</script>
